#ifndef ANDROID_NODE_H
#define ANDROID_NODE_H

/*
 * @file    harris_node.h
 *
 * Created on: Mar 10, 2012
 * Authors: Zeddy Chirombe, Alex Drake 
 *          {i.z.chirombe, alex.drake}@wustl.edu
 *
 * This file carries our Linux/Android Bridge implementation. On the Linux
 * side, it relys on ROS message service as the communication medium to 
 * interact with all other nodes that wish to engage in voice aided 
 * Robot/Human iteraction. These nodes interested in Android services, must
 * subscribe to android topic, and must advertize their unique inbox where
 * response will be send out. 
 *
 */


#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <netinet/in.h>
#include <netdb.h> 
#include <errno.h> 
#include <unistd.h>

#include <iostream>
#include <string>
#include <map>
#include <iterator>


#include "message.h"
#include "android_queue.h"


using namespace std;
const int MAX_MSG_SIZE = 256;
extern int errno;


// centralized queue to hold all incoming android based questions.
typedef map<uint32_t, Message> message_depo_h;

enum NetStatusCodes {
    SUCCESS, /* keep going */
    TIMEOUT, /* Timeout, decide whether to keep waiting for abandon request*/ 
    SOCKET_CLOSED, /* remote android server as died, change state to connect mode */
    MALFORMED_MSG /* Message Rejected, */
}; 

class AndroidNode {
public:
    AndroidNode(char* hostname, int port);
    ~AndroidNode();
    int open();
    int send_msg(Message *hMsg);
    int recv_msg(Message* hMsg, int timeout);
    void androidBridge(Message& msg, int ttl); 
    void showBiometrics(Message& msg);
    void voiceBox();
    void usage();
    string itos(int i); 
         
    /* client Thread Entry Point */
    /*static void * _android_read_(void *This)
    {
        AndroidNode *pThis = (AndroidNode *)This;
        //pThis->androidBridge();
        return NULL;
    } */
    static int message_counter;

private:
    bool chanState;
    string hostname;
    int sockfd, port, n;
    struct sockaddr_in serv_addr;
    struct hostent *server;
    char buffer[MAX_MSG_SIZE];
   
    //park all incoming requests in case we need to resend. Drop them as soon as we send reply -  
    pthread_t request_deport_t;
    pthread_mutex_t request_deport_mtx;
};

#endif
